package com.fary.resource;

import org.springframework.core.io.FileSystemResource;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;


public class TestFileSystemResource {

	/**
	 * FileSystemResource可以用来获取文件系统里面的资源，对于FileSystemResource而言我们可以获取到其对应的输出流。
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		FileSystemResource resource = new FileSystemResource("E:\\SourceCode\\Spring\\spring-framework-5.1.x\\spring-core-test\\src\\main\\resources\\resource.txt");
		if (resource.isReadable()) {
			//FileInputStream
			PrintResource.printContent(resource.getInputStream());
		}
		if (resource.isWritable()) {
			//每次都会获取到一个新的输出流
			OutputStream os = resource.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
			bw.write("你好，中国！");
			bw.flush();
			if (os != null) {
				os.close();
			}
			if (bw != null) {
				bw.close();
			}
		}
	}
}
